<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入商家手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家微信号" prop="wxAccount">
        <el-input
          v-model="queryParams.wxAccount"
          placeholder="请输入商家微信号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务状态ID" prop="serviceStatusId">
        <el-input
          v-model="queryParams.serviceStatusId"
          placeholder="请输入服务状态ID:2正常/3欠费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入商家身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:merchant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:merchant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:merchant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:merchant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家ID，主键" align="center" prop="merchantId" />
      <el-table-column label="商家名称" align="center" prop="merchantName" />
      <el-table-column label="商家手机号" align="center" prop="phone" />
      <el-table-column label="商家微信号" align="center" prop="wxAccount" />
      <el-table-column label="商家营业执照照片" align="center" prop="licensePhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.licensePhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="服务状态" align="center" prop="serviceStatusId" >
         <!-- 插槽 -->
         <template slot-scope="scope">
          
          <span v-if="scope.row.serviceStatusId=='3'">
            <el-tag type="info">欠费</el-tag>
          </span>
          <span v-if="scope.row.serviceStatusId=='2'">
            <el-tag>正常</el-tag>
          </span>
          
        </template>
      </el-table-column>

      <el-table-column label="商家身份证号" align="center" prop="idcard" />

      <el-table-column label="审核状态" align="center" prop="isPassing" >
        <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isPassing"
              active-color="#409EFF"
              inactive-color="#909399"

              :active-value=1
              :inactive-value=0
              @change="updateMerchantIsPassing(scope.row)" >
            </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="登录的启用禁用" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tooltip :content="'灰色代表禁用'" placement="top">
            <el-switch
              v-model="scope.row.status"
              inactive-color="#409EFF"    
              active-color="#909399"
              :active-value=1
              :inactive-value=0
              @change="updateMerchantStatus(scope.row)" >
            </el-switch>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:merchant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:merchant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="商家手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入商家手机号" />
        </el-form-item>
        <el-form-item label="商家微信号" prop="wxAccount">
          <el-input v-model="form.wxAccount" placeholder="请输入商家微信号" />
        </el-form-item>
        <el-form-item label="商家营业执照照片" prop="licensePhoto">
          <image-upload v-model="form.licensePhoto"/>
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="服务状态ID" prop="serviceStatusId">
          <el-input v-model="form.serviceStatusId" placeholder="请输入服务状态ID:2正常/3欠费" />
        </el-form-item>
        <el-form-item label="商家身份证号" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入商家身份证号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMerchant, getMerchant, delMerchant, addMerchant, updateMerchant } from "@/api/system/merchant";

export default {
  name: "Merchant",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家信息管理表格数据
      merchantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantName: null,
        phone: null,
        wxAccount: null,
        contactPerson: null,
        serviceStatusId: null,
        idcard: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantName: [
          { required: true, message: "商家名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "商家手机号不能为空", trigger: "blur" }
        ],
        wxAccount: [
          { required: true, message: "商家微信号不能为空", trigger: "blur" }
        ],
        licensePhoto: [
          { required: true, message: "商家营业执照照片不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        idcard: [
          { required: true, message: "商家身份证号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商家信息管理列表 */
    getList() {
      this.loading = true;
      listMerchant(this.queryParams).then(response => {
        this.merchantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 审核状态改变 */
    updateMerchantIsPassing(row){
      let data2 = {
        merchantId: row.merchantId,
        isPassing: row.isPassing
      }
      updateMerchant(data2).then(response => {
        this.getList();
      });
    },
    /** 启用禁用 */
    updateMerchantStatus(row){
      let data = {
        merchantId: row.merchantId,
        status: row.status
      }
      updateMerchant(data).then(response => {
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        merchantId: null,
        merchantName: null,
        phone: null,
        wxAccount: null,
        licensePhoto: null,
        contactPerson: null,
        serviceStatusId: null,
        idcard: null,
        templateId: null,
        isSingleDatabase: null,
        databaseId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isPassing: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.merchantId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家信息管理";
    },
    

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const merchantId = row.merchantId || this.ids
      getMerchant(merchantId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.merchantId != null) {
            updateMerchant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMerchant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const merchantIds = row.merchantId || this.ids;
      this.$modal.confirm('是否确认删除商家信息管理编号为"' + merchantIds + '"的数据项？').then(function() {
        return delMerchant(merchantIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/merchant/export', {
        ...this.queryParams
      }, `merchant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
